
package xbean;

import java.util.LinkedList;
import xdb.Bean;
import xdb.Pair;

public final class Pod {

public static boolean useBeanPool = false;

	private static ThreadLocal<LinkedList<Pair<Bean, xdb.util.BeanPool<?>>>> pBeans = new ThreadLocal<LinkedList<Pair<Bean, xdb.util.BeanPool<?>>>>() {
		@Override
		protected LinkedList<Pair<Bean, xdb.util.BeanPool<?>>> initialValue() {
			return new LinkedList<Pair<Bean, xdb.util.BeanPool<?>>>();
		}
	};

	public static void AddBeans(Bean b, xdb.util.BeanPool<?> bp) {
		if(useBeanPool == false) {
			return;
		}
		if(Thread.currentThread().getName().startsWith("xdb.Worker.protocol") || Thread.currentThread().getName().startsWith("xdb.Worker.procedure")) {
			LinkedList<Pair<Bean, xdb.util.BeanPool<?>>> bps = pBeans.get();
			bps.add(new Pair<Bean, xdb.util.BeanPool<?>>(b, bp));
		}
	}

	public static void ReuseBeans() {
		LinkedList<Pair<Bean, xdb.util.BeanPool<?>>> bps = pBeans.get();
		if(bps.size() > 0) {
			pBeans.remove();
			for (Pair<Bean, xdb.util.BeanPool<?>> e : bps) {
				@SuppressWarnings("unchecked")
				final xdb.util.BeanPool<Bean> p = (xdb.util.BeanPool<Bean>)e.second;
				p._reset_unsafe_add_(e.first);
			}
			bps.clear();
		}
	}

	public static xdb.util.BeanPool<xbean.__.ServerId.Data> poolServerId = new xdb.util.BeanPool<xbean.__.ServerId.Data>() {
		@Override
		protected xbean.__.ServerId.Data newBean() {
			return new xbean.__.ServerId.Data();
		}
	};

	public static ServerId newServerId() {
		return new xbean.__.ServerId();
	}

	public static void _reset_unsafe_add_(xbean.__.ServerId.Data bean) {
		poolServerId._reset_unsafe_add_(bean);
	}

	public static ServerId newServerIdData() {
		ServerId tmp = poolServerId.get();
		AddBeans(tmp, xbean.Pod.poolServerId);
		return tmp;
	}

	public static xdb.util.BeanPool<xbean.__.Token.Data> poolToken = new xdb.util.BeanPool<xbean.__.Token.Data>() {
		@Override
		protected xbean.__.Token.Data newBean() {
			return new xbean.__.Token.Data();
		}
	};

	public static Token newToken() {
		return new xbean.__.Token();
	}

	public static void _reset_unsafe_add_(xbean.__.Token.Data bean) {
		poolToken._reset_unsafe_add_(bean);
	}

	public static Token newTokenData() {
		Token tmp = poolToken.get();
		AddBeans(tmp, xbean.Pod.poolToken);
		return tmp;
	}

	public static xdb.util.BeanPool<xbean.__.User.Data> poolUser = new xdb.util.BeanPool<xbean.__.User.Data>() {
		@Override
		protected xbean.__.User.Data newBean() {
			return new xbean.__.User.Data();
		}
	};

	public static User newUser() {
		return new xbean.__.User();
	}

	public static void _reset_unsafe_add_(xbean.__.User.Data bean) {
		poolUser._reset_unsafe_add_(bean);
	}

	public static User newUserData() {
		User tmp = poolUser.get();
		AddBeans(tmp, xbean.Pod.poolUser);
		return tmp;
	}

	public static xdb.util.BeanPool<xbean.__.Role.Data> poolRole = new xdb.util.BeanPool<xbean.__.Role.Data>() {
		@Override
		protected xbean.__.Role.Data newBean() {
			return new xbean.__.Role.Data();
		}
	};

	public static Role newRole() {
		return new xbean.__.Role();
	}

	public static void _reset_unsafe_add_(xbean.__.Role.Data bean) {
		poolRole._reset_unsafe_add_(bean);
	}

	public static Role newRoleData() {
		Role tmp = poolRole.get();
		AddBeans(tmp, xbean.Pod.poolRole);
		return tmp;
	}

}
